package com.marktotop.controller.system;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.marktotop.annotation.LogAnnotation;
import com.marktotop.entity.system.User;
import com.marktotop.enums.UserTypeEnum;
import com.marktotop.error.SystemException;
import com.marktotop.response.ResultBean;
import com.marktotop.response.ResultCode;
import com.marktotop.service.IMenuService;
import com.marktotop.service.IRoleService;
import com.marktotop.service.IUserService;
import com.marktotop.vo.system.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author MarkToTop
 * @since 2021-07-12
 */
@RestController
@RequestMapping("/system/users")
@Api(tags = "系统模块-用户相关接口")
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private IMenuService menuService;


    /**
     * 用户列表(分页)
     *
     * @return
     */
    @ApiOperation(value = "用户列表", notes = "模糊查询用户列表")
    @RequestMapping(value = "/findUserList",method = RequestMethod.GET)
    public ResultBean findUserList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                   @RequestParam(value = "pageSize", defaultValue = "8") Integer pageSize,
                                   UserVO userVO) {
        return userService.findUserList(pageNum, pageSize, userVO);
    }


    /**
     * 用户列表，供其他调用(不分页)
     *
     * @return
     * @throws SystemException
     */
    @ApiOperation(value = "用户列表,无参", notes = "用户列表，供其他调用(不分页)")
    @RequestMapping(value = "/findListToOther",method = RequestMethod.GET)
    public ResultBean findUserLists() throws SystemException {
        List<UserContactVO> list = userService.selectAllUserContact();
        if (list.isEmpty()){
            return ResultBean.error().message("用户列表信息为空!");
        }
        return ResultBean.ok().data("result",list).message("获取用户列表信息成功!");
    }


    /**
     * 添加用户信息
     * @param userVO
     * @return
     */
//    @LogAnnotation(module = "用户模块", operator = "添加用户操作")
    @ApiOperation(value = "添加用户", notes = "添加用户信息")
    @RequiresPermissions({"user:add"})
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public ResultBean addUser(@RequestBody @Validated UserVO userVO) throws SystemException {
        if (!userService.addUser(userVO)){
            return ResultBean.error().message("添加用户信息失败！");
        }
        return ResultBean.ok().message("添加用户信息成功！");
    }

    /**
     * 更新用户
     *
     * @param id
     * @param userEditVO
     * @return
     */
    @ApiOperation(value = "更新用户", notes = "更新用户信息")
    @RequiresPermissions({"user:update"})
    @RequestMapping(value = "/update/{id}",method = RequestMethod.PUT)
    public ResultBean update(@PathVariable Long id, @RequestBody @Validated UserEditVO userEditVO) throws SystemException {
        if (!userService.updateUser(id, userEditVO)){
            return ResultBean.error().message("更新用户信息失败!");
        }
        return ResultBean.ok().message("更新用户信息成功!");
    }


    /**
     * 编辑用户
     * @param id
     * @return
     */
    @ApiOperation(value = "编辑用户", notes = "获取用户的详情，编辑用户信息")
    @RequiresPermissions({"user:edit"})
    @RequestMapping(value = "/edit/{id}",method = RequestMethod.GET)
    public ResultBean edit(@PathVariable Long id) throws SystemException {
        return ResultBean.ok().data("user",userService.editUser(id));
    }


    /**
     * 删除用户
     *
     * @param id 用户ID
     * @return
     */
    @ApiOperation(value = "删除用户", notes = "删除用户信息，根据用户ID")
    @RequiresPermissions({"user:delete"})
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)
    public ResultBean deleteUser(@PathVariable Long id) throws SystemException {
        boolean b = userService.deleteUser(id);
        System.out.println(b);
        if (!b){
            return ResultBean.error().message("删除用户信息失败!");
        }
        return ResultBean.ok().message("删除用户信息成功!");
    }


    /**
     * 获取用户信息
     *
     * @return
     */
//    @LogAnnotation(module = "用户模块", operator = "查询用户信息操作")
    @ApiOperation(value = "用户信息", notes = "用户登入信息")
    @RequestMapping(value = "/info",method = RequestMethod.GET)
    public ResultBean info() throws SystemException {
        UserInfoVO info = userService.info();
        if (null == info){
            return ResultBean.result(ResultCode.NON_RESULT);
        }
        return ResultBean.ok().data("info",info).message("用户登入信息获取成功!");
    }


    /**
     * 加载菜单
     *
     * @return
     */
    @ApiOperation(value = "加载菜单", notes = "用户登入后,根据角色加载菜单树")
    @RequestMapping(value = "/findMenu",method = RequestMethod.GET)
    public ResultBean findMenuList() {
        List<MenuNodeVO> menuTreeVOS = menuService.findMenu();
        return ResultBean.ok().data("menus",menuTreeVOS);
    }


    /**
     * 分配角色
     *
     * @param id
     * @param rids
     * @return
     */
//    @LogAnnotation(module = "用户模块", operator = "分配角色操作")
    @ApiOperation(value = "分配角色", notes = "将角色分配给用户")
    @RequiresPermissions({"user:assign"})
    @RequestMapping(value = "/{id}/assignRoles",method = RequestMethod.POST)
    public ResultBean assignRoles(@PathVariable Long id, @RequestBody Long[] rids) throws SystemException {
        if (!userService.assignRoles(id, rids)){
            return ResultBean.error().message("分配角色操作失败!");
        }
        return ResultBean.ok().message("分配角色操作成功!");
    }


    /**
     * 更新用户状态
     *
     * @param id
     * @param status
     * @return
     */
//    @LogAnnotation(module = "用户模块", operator = "更新用户状态操作")
    @ApiOperation(value = "更新用户状态", notes = "对于用户状态，禁用和启用两种")
    @RequiresPermissions({"user:status"})
    @RequestMapping(value = "/updateStatus/{id}/{status}",method = RequestMethod.PUT)
    public ResultBean updateStatus(@PathVariable Long id, @PathVariable Boolean status) throws SystemException {
        if (!userService.updateStatus(id, status)){
            return ResultBean.error().message("更新用户状态失败!");
        }
        return ResultBean.ok().message("更新用户状态操作成功!");
    }
}
